package com.whitedust.top30;

import com.sun.source.doctree.SummaryTree;

import java.util.ArrayList;
import java.util.List;

/**
 * @author A
 * @since 2025/11/3 19:15
 */

public class Top24 {

    public List<Integer> spiralOrder(int[][] matrix) {
        if (matrix.length == 0) return new ArrayList<>();
        List<Integer> res = new ArrayList<>();
        //左闭右闭来进行模拟
        int t = 0;
        int b = matrix.length - 1;
        int l = 0;
        int r = matrix[0].length - 1;
        //一行和一列都完整的吃掉
        /*
            1 2 3
            4 5 6
            7 8 9
            123
            69
            87
            4
         */
        while (true) {
            for (int i = l; i <= r; i++) res.add(matrix[t][i]);
            if (++t > b) break;
            for (int i = t; i <= b; i++) res.add(matrix[i][r]);
            if (--r < l) break;
            for (int i = r; i >= l; i--) res.add(matrix[b][i]);
            if (--b < t) break;
            for (int i = b; i >= t; i--) res.add(matrix[i][l]);
            if (++l > r) break;
        }
        return res;
    }
}
